﻿using System.Drawing;
using System.Web.UI;

namespace Sunyi.Project.WebPoint.WebControl
{
    public static class WebControlCommon
    {
        public const string ControlCssStyle = "style";
        public const string PermissionType = "mode";

        public static Color WarningBackColor = Color.PaleGoldenrod;
        public static Color DefaultControlBackColor = Color.Empty;

        public static void ShowClientMessage(Control control, string message)
        {
            message = FormatJavaScriptMessage(message);
            ScriptManager.RegisterClientScriptBlock(control, typeof(Control), "ShowClientMessage", "alert('" + message + "');", true);
        }

        public static void ClientRedirect(Control control, string url)
        {
            url = FormatJavaScriptMessage(url);
            ScriptManager.RegisterClientScriptBlock(control, typeof(Control), "ClientRedirect", "window.location='" + url + "';", true);
        }

        public static void CloseClientWindow(Control control)
        {
            ScriptManager.RegisterClientScriptBlock(control, typeof(Control), "CloseClientWindow", "window.close();", true);
        }

        private static string FormatJavaScriptMessage(string message)
        {
            return message.Replace("'", "\\'").Replace("\"", "\\\"").Replace("\r\n", "\\r\\n");
        }
    }
}